<?php
declare (strict_types=1);

namespace app\exam\service\admin;

use app\common\library\Excel;
use app\common\service\BaseService;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * Class PaperAnswerRecord
 *
 * @package app\exam\service\admin
 */
class PaperAnswerRecord extends BaseService
{
    /**
     * @Inject()
     *
     * @var \app\exam\logic\admin\PaperAnswerRecord
     */
    protected $paperAnswerRecord;

    /**
     * 试卷作答记录
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/10/25
     *
     * @param int $userId
     * @return \think\Response
     * @throws \think\db\exception\DbException
     */
    public function recordList(int $userId)
    {
        $data = $this->paperAnswerRecord->getAnswerRecordByUserId($userId);

        return $this->parseToData($data, 1);
    }

    /**
     * 试卷作答记录
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/11/1
     *
     * @param int $paperId
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws \think\db\exception\DbException
     */
    public function paperRecordList(int $paperId)
    {
        $map   = [
            'paper_id' => $paperId
        ];
        $phone = Request::get('phone/s', '');
        $data  = $this->paperAnswerRecord->getList($map, $phone)->bindAttr('user', ['avatar_url', 'nick_name', 'phone'])->hidden(['user']);
        // 定义字段
        $keyList = [
            'user'        => ListItem::make('custom')->title('用户'),
            'paper_title' => ListItem::make()->title('试卷名称'),
            'paper_score' => ListItem::make()->title('总分数'),
            'user_score'  => ListItem::make()->title('得分'),
            'time_takes'  => ListItem::make()->title('用时'),
            'topic_count' => ListItem::make()->title('题目总数'),
            'right_count' => ListItem::make()->title('正确数'),
            'commit_time' => ListItem::make()->title('交卷时间')
        ];

        $searchKey = [
            'phone' => FormItem::make('input')->title(false)->placeholder('学员手机号')->maxlength(30),
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();
    }

    /**
     * 导出选中的记录
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/11/1
     *
     * @param int $paperId
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function exportRecord(int $paperId)
    {
        $ids   = Request::post('records', '');
        $ids   = $ids ? array_unique(array_filter(explode(',', $ids))) : [];
        $datas = $this->paperAnswerRecord->getExportData($paperId, $ids);
        // 获取课程名称
        $title   = \app\exam\model\admin\Paper::where('id', $paperId)->value('title');
        $xlsCell = [
            ['phone', '手机号'],
            // ['paper_title', '试卷名称'],
            ['paper_score', '总分数'],
            ['user_score', '得分'],
            ['time_takes', '用时'],
            ['topic_count', '题目总数'],
            ['right_count', '正确数'],
            ['commit_time', '交卷时间']
        ];

        return Excel::exportExcel($title . '- 考试记录', $xlsCell, $datas);
    }

    /**
     * 导出所有考试记录
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/11/1
     *
     * @param int $paperId
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function exportRecords(int $paperId)
    {
        $datas = $this->paperAnswerRecord->getExportData($paperId);
        // 获取课程名称
        $title   = \app\exam\model\admin\Paper::where('id', $paperId)->value('title');
        $xlsCell = [
            ['phone', '手机号'],
            // ['paper_title', '试卷名称'],
            ['paper_score', '总分数'],
            ['user_score', '得分'],
            ['time_takes', '用时'],
            ['topic_count', '题目总数'],
            ['right_count', '正确数'],
            ['commit_time', '交卷时间']
        ];

        return Excel::exportExcel($title . '- 考试记录', $xlsCell, $datas);
    }

    /**
     * 答题记录信息
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/11/3
     *
     * @param int $paperId
     * @param int $recordId
     * @return \think\Response
     */
    public function recordDetails(int $paperId, int $recordId)
    {
        $where = [
            'paper_id' => $paperId,
            'id'       => $recordId
        ];

        $datas = $this->paperAnswerRecord->getInfoByWhere($where);

        return $this->parseToData($datas, 1);
    }
}